# Python 2.6.4
# Project Euler, Problem 60
# Copyright 2010 Talha Zaman

from mine import sieveprimes, isprime

def check(pr):
        return all(cat[i][j] for i in pr for j in pr if i!=j)

lim = 10000
p = sieveprimes(lim)
cat = [[isprime(int(str(i)+str(j))) and isprime(int(str(j)+str(i))) for j in p] for i in p]

for i in range(len(p)):
    for j in range(i+1, len(p)):
        if not check([i,j]): continue
        for k in range(j+1, len(p)):
            if not check([i,j,k]): continue
            for l in range(k+1, len(p)):
                if not check([i,j,k,l]): continue
#                print p[i],p[j],p[k],p[l]
                for m in range(l+1, len(p)):
                    if not check([i,j,k,l,m]): continue
                    print p[i],p[j],p[k],p[l],p[m]
                    break
